﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Address_Book
{
    public partial class frmMain : Form
    {
        private DataGridView[] dataGridView;
        private TabPage[] tabPage;
        public frmMain()
        {
            frmLogin nForm = new frmLogin();    //获取登录窗体实例
            if (nForm.ShowDialog() != DialogResult.OK)    //如果登录窗体的返回值不等于OK
            {
                Environment.Exit(Environment.ExitCode);    //退出程序
                return;
            }
            
            InitializeComponent();    //构造主窗体

            //将27个dataGridView装载入dataGridView[]数组中。
            dataGridView = new DataGridView[27];
            dataGridView[0] = this.dataGridView1;
            dataGridView[1] = this.dataGridView2;
            dataGridView[2] = this.dataGridView3;
            dataGridView[3] = this.dataGridView4;
            dataGridView[4] = this.dataGridView5;
            dataGridView[5] = this.dataGridView6;
            dataGridView[6] = this.dataGridView7;
            dataGridView[7] = this.dataGridView8;
            dataGridView[8] = this.dataGridView9;
            dataGridView[9] = this.dataGridView10;
            dataGridView[10] = this.dataGridView11;
            dataGridView[11] = this.dataGridView12;
            dataGridView[12] = this.dataGridView13;
            dataGridView[13] = this.dataGridView14;
            dataGridView[14] = this.dataGridView15;
            dataGridView[15] = this.dataGridView16;
            dataGridView[16] = this.dataGridView17;
            dataGridView[17] = this.dataGridView18;
            dataGridView[18] = this.dataGridView19;
            dataGridView[19] = this.dataGridView20;
            dataGridView[20] = this.dataGridView21;
            dataGridView[21] = this.dataGridView22;
            dataGridView[22] = this.dataGridView23;
            dataGridView[23] = this.dataGridView24;
            dataGridView[24] = this.dataGridView25;
            dataGridView[25] = this.dataGridView26;
            dataGridView[26] = this.dataGridView27;

            //将27个tabPage 装载入tabPage[] 数组中
            tabPage = new TabPage[27];
            tabPage[0] = this.tabPage1;
            tabPage[1] = this.tabPage2;
            tabPage[2] = this.tabPage3;
            tabPage[3] = this.tabPage4;
            tabPage[4] = this.tabPage5;
            tabPage[5] = this.tabPage6;
            tabPage[6] = this.tabPage7;
            tabPage[7] = this.tabPage8;
            tabPage[8] = this.tabPage9;
            tabPage[9] = this.tabPage10;
            tabPage[10] = this.tabPage11;
            tabPage[11] = this.tabPage12;
            tabPage[12] = this.tabPage13;
            tabPage[13] = this.tabPage14;
            tabPage[14] = this.tabPage15;
            tabPage[15] = this.tabPage16;
            tabPage[16] = this.tabPage17;
            tabPage[17] = this.tabPage18;
            tabPage[18] = this.tabPage19;
            tabPage[19] = this.tabPage20;
            tabPage[20] = this.tabPage21;
            tabPage[21] = this.tabPage22;
            tabPage[22] = this.tabPage23;
            tabPage[23] = this.tabPage24;
            tabPage[24] = this.tabPage25;
            tabPage[25] = this.tabPage26;
            tabPage[26] = this.tabPage27;

        }

        int flag = 0;//标志位：0为初始状态，1为添加好友状态，2为编辑好友状态，3为删除好友状态。
        Address_Book.Model.FriendInfo Finfo = new Address_Book.Model.FriendInfo();// 好友信息。

        private void frmMain_Load(object sender, EventArgs e)
        {
            checkFlag(flag);//刷新网格数据

            for (int i = 0; i <= 26;i++ )//关联各dataGridView单击事件。
                this.dataGridView[i].Click += new System.EventHandler(this.dataGridView_Click);
        }


        //窗体操作状态设置
        private void checkFlag(int flag)
        {
            switch (flag)
            {
                case 0:
                    //刷新网格数据
                    for (int i = 0; i <= 26; i++)
                    {
                        Address_Book.BLL.RefreshData refData = new Address_Book.BLL.RefreshData();
                        DataSet ds = new DataSet();
                        ds = refData.RefreshView(i);
                        dataGridView[i].DataSource = ds.Tables[0];//填充数据
                        dataGridView[i].SelectionMode = DataGridViewSelectionMode.FullRowSelect;//设置鼠标单击选择一行
                        dataGridView[i].AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);//设置列宽为自动调整模式
                        dataGridView[i].ReadOnly = true;//设置单元格为只读模式
                        dataGridView[i].AllowUserToAddRows = false;//设置用户不能添加新行，达到隐藏最后带*号的空行
                    }

                    btnOK.Enabled = false;
                    btnCancel.Enabled = false;
                    btnCls.Enabled = false;
                    btnAdd.Enabled = true;
                    btnEdit.Enabled = true;
                    btnDelete.Enabled = true;

                    txtName.Enabled = false;
                    cmbSex.Enabled = false;
                    txtBirthday.Enabled = false;
                    txtTel1.Enabled = false;
                    txtTel2.Enabled = false;
                    txtHomeTel.Enabled = false;
                    txtQQ.Enabled = false;
                    txtEmail.Enabled = false;
                    txtAddress.Enabled = false;
                    cmbLelever.Enabled = false;
                    txtNote.Enabled = false;

                    muAdd.Enabled = true;
                    muEdit.Enabled = true;
                    muDelete.Enabled = true;
                    muOpera.Enabled = true;

                    tabControl2.Enabled = true;
                    break;
                case 1:
                //添加、编辑的设置相同    
                case 2:
                    btnOK.Enabled = true;
                    btnCancel.Enabled = true;
                    btnCls.Enabled = true;
                    btnAdd.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;

                    txtName.Enabled = true;
                    cmbSex.Enabled = true;
                    txtBirthday.Enabled = true;
                    txtTel1.Enabled = true;
                    txtTel2.Enabled = true;
                    txtHomeTel.Enabled = true;
                    txtNote.Enabled = true;
                    txtQQ.Enabled = true;
                    txtEmail.Enabled = true;
                    txtAddress.Enabled = true;
                    cmbLelever.Enabled = true;
                    txtNote.Enabled = true;

                    muAdd.Enabled = false;
                    muEdit.Enabled = false;
                    muDelete.Enabled = false;
                    muOpera.Enabled = false;

                    cmbSex.Items.Clear();
                    cmbLelever.Items.Clear();
                    cmbSex.Items.Add("男");
                    cmbSex.Items.Add("女");
                    cmbLelever.Items.Add("好兄弟");
                    cmbLelever.Items.Add("好姐妹");

                    tabControl2.Enabled = false;
                    break;
                case 3:
                    btnOK.Enabled = true;
                    btnCancel.Enabled = true;
                    btnCls.Enabled = true;
                    btnAdd.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;

                    txtName.Enabled = false;
                    cmbSex.Enabled = false;
                    txtBirthday.Enabled = false;
                    txtTel1.Enabled = false;
                    txtTel2.Enabled = false;
                    txtHomeTel.Enabled = false;
                    txtQQ.Enabled = false;
                    txtEmail.Enabled = false;
                    txtAddress.Enabled = false;
                    cmbLelever.Enabled = false;
                    txtNote.Enabled = false;

                    muAdd.Enabled = false;
                    muEdit.Enabled = false;
                    muDelete.Enabled = false;
                    muOpera.Enabled = false;

                    tabControl2.Enabled = false;
                    break;
                default:
                    break;
            }
        }

        //添加好友
        private void btnAdd_Click(object sender, EventArgs e)
        {
            flag = 1;
            checkFlag(flag);
        }

        //编辑好友
        private void btnEdit_Click(object sender, EventArgs e)
        {
            flag = 2;
            checkFlag(flag);
        }

        //删除好友
        private void btnDelete_Click(object sender, EventArgs e)
        {
            flag = 3;
            checkFlag(flag);
        }

        //确定
        private void btnOK_Click(object sender, EventArgs e)
        {
            Finfo.FName = txtName.Text;
            Finfo.FSex = cmbSex.Text;
            Finfo.FBirthday = txtBirthday.Text.Trim();
            Finfo.FTel1 = txtTel1.Text;
            Finfo.FTel2 = txtTel2.Text;
            Finfo.FHomeTel = txtHomeTel.Text;
            Finfo.FQQ = txtQQ.Text;
            Finfo.FE_Mail = txtEmail.Text;
            Finfo.FAddress = txtAddress.Text;
            Finfo.FLelever = cmbLelever.Text;
            Finfo.FNote = txtNote.Text;
            Address_Book.BLL.Operate operate = new Address_Book.BLL.Operate();
            if (operate.Exct(flag, Finfo))
            {
                switch (flag)//成功后返回操作信息。
                {
                    case 1:
                        MessageBox.Show("添加成功！");
                        break;
                    case 2:
                        MessageBox.Show("编辑成功！");
                        break;
                    case 3:
                        MessageBox.Show("删除成功！");
                        break;
                    default:
                        break;
                }
                flag = 0;
                checkFlag(flag);
            }
            else
            {
                MessageBox.Show("操作失败，请检查输入数据的有效性！");
            }
        }

        //取消操作
        private void btnCancel_Click(object sender, EventArgs e)
        {
            flag = 0;
            checkFlag(flag);
        }

        //重置数据
        private void btnCls_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            cmbSex.Text = "";
            cmbSex.Items.Clear();
            cmbSex.Items.Add("男");
            cmbSex.Items.Add("女");
            txtBirthday.Text = "";
            txtTel1.Text = "";
            txtTel2.Text = "";
            txtHomeTel.Text = "";
            txtQQ.Text = "";
            txtEmail.Text = "";
            txtAddress.Text = "";
            cmbLelever.Text = "";
            cmbLelever.Items.Clear();
            cmbLelever.Items.Add("好兄弟");
            cmbLelever.Items.Add("好姐妹");
            txtNote.Text = "";
        }

        //退出
        private void muQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //点击dataGridView关联其它控件数据。
        private void dataGridView_Click(object sender, EventArgs e)
        {
            int i = tabControl2.SelectedIndex;
            
            Finfo.FID = Convert.ToInt32(dataGridView[i][0, dataGridView[i].CurrentCell.RowIndex].Value);
            Finfo.FName = dataGridView[i][1, dataGridView[i].CurrentCell.RowIndex].Value.ToString();
            Finfo.FSex = dataGridView[i][2, dataGridView[i].CurrentCell.RowIndex].Value.ToString();
            Finfo.FBirthday = dataGridView[i][3, dataGridView[i].CurrentCell.RowIndex].Value.ToString();
            Finfo.FTel1 = dataGridView[i][4, dataGridView[i].CurrentCell.RowIndex].Value.ToString();
            Finfo.FTel2 = dataGridView[i][5, dataGridView[i].CurrentCell.RowIndex].Value.ToString();
            Finfo.FHomeTel = dataGridView[i][6, dataGridView[i].CurrentCell.RowIndex].Value.ToString();
            Finfo.FQQ = dataGridView[i][7, dataGridView[i].CurrentCell.RowIndex].Value.ToString();
            Finfo.FE_Mail = dataGridView[i][8, dataGridView[i].CurrentCell.RowIndex].Value.ToString();
            Finfo.FAddress = dataGridView[i][9, dataGridView[i].CurrentCell.RowIndex].Value.ToString();
            Finfo.FLelever = dataGridView[i][10, dataGridView[i].CurrentCell.RowIndex].Value.ToString();
            Finfo.FNote = dataGridView[i][11, dataGridView[i].CurrentCell.RowIndex].Value.ToString();

            txtName.Text = Finfo.FName;
            cmbSex.Text = Finfo.FSex;
            txtBirthday.Text = Finfo.FBirthday;
            txtTel1.Text = Finfo.FTel1;
            txtTel2.Text = Finfo.FTel2;
            txtHomeTel.Text = Finfo.FHomeTel;
            txtQQ.Text = Finfo.FQQ.ToString();
            txtEmail.Text = Finfo.FE_Mail;
            txtAddress.Text = Finfo.FAddress;
            cmbLelever.Text = Finfo.FLelever;
            txtNote.Text = Finfo.FNote;
        }

        private void muImport_Click(object sender, EventArgs e)
        {
            frmImport frmimport = new frmImport();
            frmimport.ShowDialog(this);
        }

        private void muExport_Click(object sender, EventArgs e)
        {
            frmExport frmexport = new frmExport();
            frmexport.ShowDialog(this);
        }

        private void muAbout_Click(object sender, EventArgs e)
        {
            MessageBox("版本号：1.0.0.1 \n\n作者：地平线↑\nE_mail:511576392@qq.com", "关于我！");
        }
    }
}